package cn.jhz.learn.blog.service.security.resource.article.impl;

import cn.jhz.learn.blog.common.exception.ParamException;
import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.service.security.resource.article.ArticleValidationService;
import cn.jhz.learn.blog.service.security.resource.article.SubfieldValidationService;
import cn.jhz.learn.blog.vo.article.query.page.ArticlePageView;
import cn.jhz.learn.blog.vo.article.submit.ArticleParam;
import cn.jhz.learn.blog.vo.article.query.page.ConsolePageView;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ArticleValidationAspect extends ArticlePointCutImpl {

    private final ArticleValidationService validationService;
    private final SubfieldValidationService subfieldValidationService;

    @Autowired
    public ArticleValidationAspect(ArticleValidationService validationService, SubfieldValidationService subfieldValidationService) {
        this.validationService = validationService;
        this.subfieldValidationService = subfieldValidationService;
    }

    @Before(value = "create(uid, param)", argNames = "uid,param")
    public void createParamVerification(Integer uid, @NotNull ArticleParam param){
        validationService.blogValid(uid);
        if(!param.getType().equals(GLOBAL.PARAM.SUBMIT.ORIGINAL))
            if(!param.getLink().isPresent())
                throw new ParamException("文章缺少引用链接");
            else if(!param.getConfirm().isPresent())
                throw new ParamException("文章未获得原作者授权");
        if(param.getSubfield().isPresent())
            for(Integer subfieldId : param.getSubfield().get())
                subfieldValidationService.validStrict(uid, subfieldId);
    }

    @Before(value = "delete(uid, id, deep)", argNames = "uid,id,deep")
    public void deleteParamVerification(Integer uid, Integer id, Boolean deep){
        validationService.validStrict(uid, id);
    }

    @Before(value = "update(uid, param)", argNames = "uid,param")
    public void updateParamVerification(Integer uid, @NotNull ArticleParam param){
        if(param.getId().isPresent())
            validationService.validStrict(uid, param.getId().get());
        else
            throw new ParamException("缺少文章ID!");
        if(param.getSubfield().isPresent())
            for(Integer subfieldId : param.getSubfield().get())
                subfieldValidationService.validStrict(uid, subfieldId);
        if(!param.getType().equals(GLOBAL.PARAM.SUBMIT.ORIGINAL)){
            if(!param.getLink().isPresent())
                throw new ParamException("文章缺少引用链接");
            else if(!param.getConfirm().isPresent())
                throw new ParamException("文章未获得原作者授权");
        }
    }

    @Before(value = "recycle(uid, id)", argNames = "uid,id")
    public void recycleParamVerification(Integer uid, Integer id){
        validationService.validStrict(uid, id);
    }

    @Before(value = "stick(uid, id, sticky)", argNames = "uid,id,sticky")
    public void stickParamVerification(Integer uid, Integer id, Boolean sticky){
        validationService.validStrict(uid, id);
    }

    @Before(value = "getPage(pageView)", argNames = "pageView")
    public void getPageParamVerification(@NotNull ArticlePageView pageView) {
        if(pageView.getBlog_name().isPresent() && pageView.getSubfield_id().isPresent())
            throw new ParamException("查询参数错误, 只能有一个关键条件!");
        else if(!pageView.getBlog_name().isPresent() && !pageView.getSubfield_id().isPresent())
            throw new ParamException("缺少条件, 至少有一个条件!");
        else if(pageView.getBlog_name().isPresent())
            validationService.blogValid(pageView.getBlog_name().get());
        else
            validationService.subfieldValid(pageView.getSubfield_id().get());

    }

    @Before(value = "getPageUid(uid,pageView)", argNames = "uid,pageView")
    public void getPageParamVerification(Integer uid, @NotNull ArticlePageView pageView) {
        if(pageView.getBlog_name().isPresent() && pageView.getSubfield_id().isPresent())
            throw new ParamException("查询参数错误, 只能有一个关键条件!");
        else if(!pageView.getBlog_name().isPresent() && !pageView.getSubfield_id().isPresent())
            throw new ParamException("缺少条件, 至少有一个条件!");
        else if(pageView.getBlog_name().isPresent())
            validationService.blogValid(uid, pageView.getBlog_name().get());
        else
            validationService.subfieldValid(uid, pageView.getSubfield_id().get());
    }

    @Before(value = "getCountAndList(uid, pageView)", argNames = "uid,pageView")
    public void getCountAndListParamVerification(Integer uid, @NotNull ConsolePageView pageView){
        if(pageView.getVisibility().isPresent() && pageView.getStatus().isPresent())
            throw new ParamException("无效参数, 仅需一个查询条件！");
        validationService.blogValid(uid);
    }

    @Before(value = "getDetail(id)", argNames = "id")
    public void getDetailParamVerification(Integer id) {
        validationService.valid(id);
    }

    @Before(value = "getDetailUid(uid,id)", argNames = "uid,id")
    public void getDetailParamVerification(Integer uid, Integer id) {
        validationService.valid(uid, id);
    }

    public void getDetailConsoleParamVerification(Integer uid, Integer id){
        validationService.validStrict(uid, id);
    }
}
